var popupManager = new function() {

  /** Back stack. */
  var backstack = [];
  /** Header view references. */
  var header = {
    backButton : null,
  };
  
  /** Current term to edit. */
  var currentTermToEdit = null;
  
  var viewSetupers = {
    '#editView' : function(selector) {
      if (!currentTermToEdit) { alert('Error! Sorry!'); return; }
      var view = $(selector);
      view.find('#editViewCaption').text(currentTermToEdit.value);
      var editViewList = view.find('#editViewList');
      editViewList.empty();
      for (var i = 0; i < currentTermToEdit.desc.length; i++) {
        addDescriptionToList(editViewList, currentTermToEdit.desc[i], i);
      }
      var input = view.find('#editViewDescription');
      input.val('');
      input.keypress(function(event) {
        if (event.which == 13) {
          event.preventDefault();
          var text = input.val();
          if (!text) { return; }
          currentTermToEdit.addDesc(text);
          addDescriptionToList(editViewList, text, currentTermToEdit.desc.length - 1);
          input.val('');
          Voc.updateTerm(currentTermToEdit);
        }
      });
    }
  }
  
  function addDescriptionToList(list, text, index) {
    if (!text) { return; }
    list.append($('<li style="display:block">')
        .append($('<span class="ui-icon ui-icon-close">')
            .css({'float' : 'left'})
            .html('&nbsp;delete').data('index', index)
            .click(function() {
              currentTermToEdit.deleteDescription(index);
              Voc.updateTerm(currentTermToEdit);
              $(this).parent().remove();
            })
        )
        .append($('<span>').text(text).css({'float' : 'left', 'margint-left' : '5px', 'max-width' : '340px'}))
        .append($('<div>').css({'clear' : 'left'}))
    );
  }
  
  function getCurrentViewSelector() {
    return backstack.length > 0 ? backstack[backstack.length - 1] : null;
  }
  function getPrevViewSelector() {
    return backstack.length > 1 ? backstack[backstack.length - 2] : null;
  }
  
  function invalidateBackButton() {
    header.backButton.css('visibility', backstack.length > 1 ? 'visible' : 'hidden');
  }
  
  function goBack() {
    console.log('Go back...');
    var current = $(getCurrentViewSelector());
    var prev = $(getPrevViewSelector());
    backstack.pop();
    hideView(current);
    showView(prev);
    invalidateBackButton();
    console.log('Stack length ' + backstack.length);
    console.log('Refreshing list of terms');
    utils.refreshList();
  }
  
  function hideView(selector) {
    $(selector).hide('slide', {}, 500);
  }

  function showView(selector) {
    $(selector).show('slide', {}, 500);
  }
  
  return {
    setupHeader : function(selector) {
      header.backButton = $(selector).find('#backButton');
      header.backButton.click(goBack);
    },
    
    nextView : function(selector) {
      console.log('Next view ' + selector);
      var currentSelector = getCurrentViewSelector();
      if (currentSelector) {
        console.log('Hide ' + currentSelector);
        hideView(currentSelector);
      }
      backstack[backstack.length] = selector;
      console.log('Stack length ' + backstack.length);
      invalidateBackButton();
      var setup = viewSetupers[selector];
      if (setup) { setup(selector); }
      showView(selector);
    },
    
    finish : function() { goBack(); },
    
    setCurrentTerm : function(term) { currentTermToEdit = term; },
    
    // Refresh list if global array of terms has been changed
    refreshList : function() {
      
    }
  }
  
}
